import { Effect, ImmerReducer, Reducer, Subscription } from 'umi';
import {vist,det} from '../services/index'
export interface IndexModelState {
list:any[];
num:[]
}

export interface IndexModelType {
  namespace: 'visit';
  state: IndexModelState;
  effects: {
    vist:Effect,
    det:Effect,
  };
  reducers: {
    save: Reducer<IndexModelState>;
    // 启用 immer 之后
    // save: ImmerReducer<IndexModelState>;
  };
  subscriptions?: { setup: Subscription };
}
const IndexModel: IndexModelType = {
  namespace: 'visit',
  state: {
    list:[],
    num:[]

  },

  //异步action
  effects: {
    *vist({ payload }, { call, put }) {
      let results = yield call(vist,payload&&payload.ip,payload&&payload.browser,payload&&payload.engine,payload&&payload.os
     ,payload&&payload.device,payload&&payload.userAgent,payload&&payload.url,payload&&payload.address);
      console.log(results)
      yield put({
        type:'save',
        payload:{
         list:results

        }
      })
    },
    *det({ payload }, { call, put }) {
        let results = yield call(det,payload.id);
        console.log(results)
        yield put({
          type:'save',
          payload:{
           num:results
  
          }
        })
      },
  },
  //同步action
  reducers: {
    save(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    },

  },
};

export default IndexModel;